package running

import (
	"context"
	"encoding/json"
	"ez/apps/running/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type AppVersion struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64 `json:"id" bson:"id,omitempty"`

	Name string `json:"name" bson:"name"` //版本描述

	App *App `json:"app" bson:"app"` //APP

	AppId int64 `json:"appId" bson:"appId"` //APP

	Dir string `json:"dir" bson:"dir"` //上传路径

	Starter string `json:"starter" bson:"starter"` //启动命令

	Config string `json:"config" bson:"config"` //配置内容

	ConfigFile string `json:"configFile" bson:"configFile"` //配置文件

	Logs string `json:"logs" bson:"logs"` //日志记录

	FtpOpen bool `json:"ftpOpen" bson:"ftpOpen"` //ftp开关

	FtpPort int64 `json:"ftpPort" bson:"ftpPort"` //ftp端口

	FtpUser string `json:"ftpUser" bson:"ftpUser"` //ftp账号

	FtpPass string `json:"ftpPass" bson:"ftpPass"` //ftp密码

	CreateAt time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *AppVersion) DocName() string { return "AppVersion" }

func (this *AppVersion) GetId() int64 { return this.Id }

func (this *AppVersion) SetId(id int64) { this.Id = id }

func (this *AppVersion) Create() error {
	return this.GetFactory().Create(this)
}

func (this *AppVersion) Replace() error {
	return this.GetFactory().Replace(this)
}

func (this *AppVersion) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *AppVersion) Delete() error {
	return this.GetFactory().Delete(this)
}

func (this *AppVersion) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *AppVersion) Destroy() error {
	return this.GetFactory().Destroy(this)
}

func (this *AppVersion) ToString() string {
	return string(this.ToBytes())
}

func (this *AppVersion) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *AppVersion) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *AppVersion) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}

func (this *AppVersion) LoadApp() {
	if this.AppId == 0 {
		return
	}
	this.App, _ = NewAppCrud().FindId(this.AppId)
}

func (this *AppVersion) ClearRelationsBeforeSave() mgo.Doc {

	this.App = nil

	return this
}

func neverUsed_AppVersion() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type AppVersionAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *AppVersion
	Session *ez.Session
}

func NewAppVersionAccessControl(model *AppVersion, action string, session *ez.Session) *AppVersionAccessControl {
	ctrl := &AppVersionAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.AppVersionAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
